package com.sky.common.track;

import com.sky.business.sys.event.CmccBeginTrackingEvent;
import com.sky.common.config.CacheConstants;
import com.sky.common.utils.DateUtils;

import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 追踪任务管家,记录追踪任务的最近下发时间,和最近追踪到的时间,如果2个时间都超过120秒，下发任务结束指令
 *
 */
public class TrackTaskManager {

    /**
     * 任务记录
     */
    private static final Map<String, TrackTaskInfo> trackTaskMap = new ConcurrentHashMap<>();


    /**
     * 设备状态记录
     */
    private static final Map<String,DeviceStatus> deviceStatusMap = new ConcurrentHashMap<>();


    /**
     * 获取设备状态
     * @param deviceId 设备id
     * @return 设备状态
     */
    public static DeviceStatus getDeviceStatus(String deviceId) {
        DeviceStatus deviceStatus = deviceStatusMap.get(deviceId);
        if (deviceStatus == null) {
            deviceStatus = new DeviceStatus();
            deviceStatusMap.put(deviceId, deviceStatus);
        }
        return deviceStatus;
    }

    /**
     * 获取任务信息
     * @param taskId 任务id
     * @return 任务信息
     */
    public static  TrackTaskInfo getTrackTaskInfo(String taskId) {
        return trackTaskMap.get(taskId);
    }


    /**
     * 删除任务
     *
     * @param taskId 任务id
     */
    public static void remove(String taskId) {
        trackTaskMap.remove(taskId);
    }

    /**
     * 更新接收任务的时间
     *
     * @param taskId   任务id
     * @param taskTime 任务下发时间
     * @param cmccBeginTrackingEvent 开始跟踪事件
     */
    public static void updateReceiveTrackTime(String taskId, Date taskTime, CmccBeginTrackingEvent cmccBeginTrackingEvent) {
        TrackTaskInfo trackTaskInfo = trackTaskMap.get(taskId);
        if(trackTaskInfo==null){
            trackTaskInfo = new TrackTaskInfo(taskId, taskTime, cmccBeginTrackingEvent);
            trackTaskMap.put(taskId,trackTaskInfo);
        }else{
            trackTaskInfo.setLastBeginTrackEvent(cmccBeginTrackingEvent);
            trackTaskInfo.setLastReceiveTaskTime(taskTime);
        }
    }

    /**
     * 更新跟踪上无人机的时间
     *
     * @param taskId      任务id
     * @param deviceId 设备id
     */
    public static void updateTrackedTime(String taskId,String deviceId) {
        Date now = new Date();
        TrackTaskInfo trackTaskInfo = trackTaskMap.get(taskId);
        if(trackTaskInfo!=null) {
            Map<String,Date> map = trackTaskInfo.getDeviceLastReceiveMap();
            map.put(deviceId,now);
        }
    }


    /**
     * 获取任务id集合
     *
     * @return 任务id集合
     */
    public static Set<String> getTrackTaskIdSet() {
        return trackTaskMap.keySet();
    }


    /**
     * 在一段时间内是否处于不活跃状态
     *
     * @param taskId         任务id
     * @param inactiveSecond 不活跃时间
     * @return 是否处于不活跃状态
     */
    public static boolean inactive(String taskId, int inactiveSecond) {
        TrackTaskInfo trackTaskInfo = trackTaskMap.get(taskId);
        if (trackTaskInfo == null) {
            return true;
        }
        Date now = new Date();
        Date lastReceiveBeginTrack = trackTaskInfo.getLastReceiveTaskTime();
        long plusSecond = DateUtils.secondPlus(now, lastReceiveBeginTrack);
        return plusSecond >= inactiveSecond;
    }

    /**
     * 设备是否在执行oe任务
     *
     * @param deviceId 设备id
     * @return 是否执行oe任务
     */
    public static  boolean inOeTask(String deviceId) {
        Date lastOeTaskDate = getDeviceStatus(deviceId).getLastOeTaskTime();
        if (lastOeTaskDate == null) {
            return false;
        }
        Date now = new Date();
        long second = DateUtils.secondPlus(now, lastOeTaskDate);
        return second < CacheConstants.EXPIRE_OE_TIME_SECOND;
    }

}
